package com.code.translate.plugins;

import com.code.translate.function.TextTranslatePlugin;
import com.code.translate.utils.HttpUtils;
import com.code.translate.utils.JsonpathUtils;
import com.code.translate.utils.MD5Utils;
import com.code.translate.utils.StringUtils;
import org.springframework.http.HttpMethod;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ggh
 * @date 2022/7/24
 */
public class JinshanTranslatePlugin extends TextTranslatePlugin {
    private static final String translateUrl = "http://ifanyi.iciba.com/index.php?c=trans&m=fy&client=%s&auth_user=%s&sign=%s";
    private static final String referUrl = "http://www.iciba.com/fy";
    private static final String client = "6";
    private static final String authUser = "key_web_fanyi";
    private static final String sourceStr = "z%C3%8F%C3%87%C3%8F%C3%A7%C3%A2%C3%A0%C3%9C%C3%87%C2%9A%C2%A0%C3%8B%C2%9C%C2%AC%C2%ACq%C2%9D";
    private static final Integer maxLen = 3000;

    private static JinshanTranslatePlugin singleton = new JinshanTranslatePlugin();

    private JinshanTranslatePlugin() {
    }

    public static JinshanTranslatePlugin getSingleton() {
        return singleton;
    }

    @Override
    protected Map<String, Object> prepare() {
        return null;
    }

    @Override
    protected String doTranslate(String text, Map<String, Object> params) throws Exception {
        String tempSign = generateSign();
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Referer", referUrl);

        if (text.length() > maxLen) {
            List<String> singleTexts = StringUtils.splitString(text, maxLen, 46);

            StringBuffer buffer = new StringBuffer();
            for (String singleText : singleTexts) {
                Map<String, Object> requestParams = structParams(singleText);

                String sign = MD5Utils.md5Hex((client + authUser + tempSign + requestParams.get("q")).getBytes());
                sign = sign.substring(0, 16);
                String tempTranslateUrl = String.format(translateUrl, client, authUser, sign);

                String response = HttpUtils.req(tempTranslateUrl, HttpMethod.POST, headers, requestParams);
                buffer.append(doExtract(response));
            }

            return buffer.toString();
        } else {
            Map<String, Object> requestParams = structParams(text);

            String sign = MD5Utils.md5Hex((client + authUser + tempSign + requestParams.get("q")).getBytes());
            sign = sign.substring(0, 16);
            String tempTranslateUrl = String.format(translateUrl, client, authUser, sign);

            String response = HttpUtils.req(tempTranslateUrl, HttpMethod.POST, headers, requestParams);

            return doExtract(response);
        }
    }

    @Override
    protected String doExtract(String response) {
        if (response.contains("\"status\":1")) {
            return JsonpathUtils.read(response, "$.content.out");
        } else {
            System.out.println(response);
            return null;
        }
    }


    private static Map<String, Object> structParams(String text) throws Exception {
        Map<String, Object> params = new HashMap<>(3);

        params.put("from", "en");
        params.put("to", "zh");
        params.put("q", text.trim());

        return params;
    }

    public static String generateSign() {
        String tempSign = URLDecoder.decode(sourceStr);

        char t = (char) (tempSign.charAt(0) - tempSign.length());
        StringBuffer sb = new StringBuffer("" + t);
        for (int r = 1; r < tempSign.length(); r++) {
            sb.append((char) (tempSign.charAt(r) - sb.charAt(r - 1)));
        }
        return sb.toString();
    }
}
